/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ModelToTree.impl;

import BoardGame.Board;
import BoardGame.Field;
import BoardGame.Figure;

import MocaTree.File;
import MocaTree.MocaTreeFactory;
import MocaTree.Node;

import ModelToTree.ModelToTreeFactory;
import ModelToTree.ModelToTreePackage;
import ModelToTree.ModelToTreeTransformer;

import ModelToTree.ModelToTreeTransformerUtils;
import de.upb.tools.sdm.*;

import java.lang.reflect.InvocationTargetException;

import java.util.*;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.impl.EObjectImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Transformer</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class ModelToTreeTransformerImpl extends EObjectImpl implements
		ModelToTreeTransformer {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ModelToTreeTransformerImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ModelToTreePackage.Literals.MODEL_TO_TREE_TRANSFORMER;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public File transformBoard(Board board) {
		boolean fujaba__Success = false;
		File file = null;
		Node root = null;
		ModelToTreeTransformerUtils utils = null;
		Node fieldNode = null;
		Iterator fujaba__IterRootToRowNode = null;
		Object _TmpObject = null;
		Node rowNode = null;
		Figure figure = null;
		Node figureNode = null;
		Iterator fujaba__IterBoardToField = null;
		Field field = null;

		// story pattern initialize file
		try {
			fujaba__Success = false;

			// check object board is really bound
			JavaSDM.ensure(board != null);
			// create object file
			file = MocaTreeFactory.eINSTANCE.createFile();
			// create object root
			root = MocaTreeFactory.eINSTANCE.createNode();
			// create object utils
			utils = ModelToTreeFactory.eINSTANCE
					.createModelToTreeTransformerUtils();
			// assign attribute file
			file.setName("board");
			// assign attribute root
			root.setName("BOARD");
			// create link
			root.setFile(file);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern iterate over nodes
		try {
			fujaba__Success = false;

			// check object board is really bound
			JavaSDM.ensure(board != null);
			// iterate to-many link fields from board to field
			fujaba__Success = false;

			fujaba__IterBoardToField = new ArrayList(board.getFields())
					.iterator();

			while (fujaba__IterBoardToField.hasNext()) {
				try {
					field = (Field) fujaba__IterBoardToField.next();

					// check object field is really bound
					JavaSDM.ensure(field != null);
					// create object fieldNode
					fieldNode = MocaTreeFactory.eINSTANCE.createNode();
					// assign attribute fieldNode
					fieldNode.setIndex(field.getX());
					// assign attribute fieldNode
					fieldNode.setName("FIELD");
					// story pattern ActivityNode1
					try {
						fujaba__Success = false;

						// check object root is really bound
						JavaSDM.ensure(root != null);
						// check negative bindings
						try {
							fujaba__Success = false;

							// iterate to-many link children from root to rowNode
							fujaba__Success = false;

							fujaba__IterRootToRowNode = root.getChildren()
									.iterator();

							while (!(fujaba__Success)
									&& fujaba__IterRootToRowNode.hasNext()) {
								try {
									_TmpObject = fujaba__IterRootToRowNode
											.next();

									// ensure correct type and really bound of object rowNode
									JavaSDM.ensure(_TmpObject instanceof Node);
									rowNode = (Node) _TmpObject;

									// check isomorphic binding between objects rowNode and root
									JavaSDM.ensure(!rowNode.equals(root));

									// attribute condition
									JavaSDM.ensure(rowNode.getIndex() == field
											.getY());

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}
							}
							JavaSDM.ensure(fujaba__Success);

							fujaba__Success = true;
						} catch (JavaSDMException fujaba__InternalException) {
							fujaba__Success = false;
						}

						fujaba__Success = !(fujaba__Success);

						JavaSDM.ensure(fujaba__Success);

						// create object rowNode
						rowNode = MocaTreeFactory.eINSTANCE.createNode();
						// assign attribute rowNode
						rowNode.setName("ROW");
						// assign attribute rowNode
						rowNode.setIndex(field.getY());
						// assign attribute rowNode
						rowNode.setName("ROW");
						// create link
						root.getChildren().add(rowNode);

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					// story pattern ActivityNode2
					try {
						fujaba__Success = false;

						// check object fieldNode is really bound
						JavaSDM.ensure(fieldNode != null);
						// check object rowNode is really bound
						JavaSDM.ensure(rowNode != null);
						// check isomorphic binding between objects rowNode and fieldNode
						JavaSDM.ensure(!rowNode.equals(fieldNode));

						// create link
						fieldNode.setParentNode(rowNode);

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					// story pattern ActivityNode4
					try {
						fujaba__Success = false;

						// check object field is really bound
						JavaSDM.ensure(field != null);
						// check object fieldNode is really bound
						JavaSDM.ensure(fieldNode != null);
						// bind object
						figure = field.getFigure();

						// check object figure is really bound
						JavaSDM.ensure(figure != null);

						// create object figureNode
						figureNode = MocaTreeFactory.eINSTANCE.createNode();
						// assign attribute figureNode
						figureNode.setName(utils.charForFigure(figure));
						// create link
						figureNode.setParentNode(fieldNode);

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return file;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eInvoke(int operationID, EList<?> arguments)
			throws InvocationTargetException {
		switch (operationID) {
		case ModelToTreePackage.MODEL_TO_TREE_TRANSFORMER___TRANSFORM_BOARD__BOARD:
			return transformBoard((Board) arguments.get(0));
		}
		return super.eInvoke(operationID, arguments);
	}

} //ModelToTreeTransformerImpl
